<?php
  //Library - gen_Table


  //these functions make it easy to draw tables
  
  //some functions are renamed ot make it easer to remeber and type. Need to change pages that use old functions


// ---------------------------------------------------------------------------------------------------------------------  
  function gentab_Start($index=0,$x,$width,$text_left=0,$text_right=0)
  {
    gen_table_start($x,$width,$index,$text_left,$text_right);
  }

  function gen_table_start($x,$width,$index=0,$text_left=0,$text_right=0)
  {
    global $gen_tables;
    $gen_tables[$index]['x']         = $x;
    $gen_tables[$index]['y']         = current_line();
    $gen_tables[$index]['width']     = $width;
    $gen_tables[$index]['txt_left']  = $text_left;
    $gen_tables[$index]['txt_right'] = $text_right;
    
    gentab_Cell_Clear($index);
    gentab_Cell_Add($index,0,$width);
  }
  
// ---------------------------------------------------------------------------------------------------------------------  
  function gentab_End($index=0) {gen_table_end($index);}
  
  function gen_table_end($index=0)
  {
    global $pdf;
    global $gen_tables;
    $height = current_line() - $gen_tables[$index]['y'];
    $cell_count = count($gen_tables[$index]['cells']);
    
    if($cell_count > 0)
    {
      $cell_count--;
      for($loop = 0;$loop < $cell_count;$loop++)
      {
        gentab_VerLine_End($index,$loop);
      }
    }
    
    $pdf->Rect($gen_tables[$index]['x'],$gen_tables[$index]['y'] - 2,$gen_tables[$index]['width'],$height);
  }
  
// ---------------------------------------------------------------------------------------------------------------------  
  function gen_table_custom_line($x,$width)
  {
    global $pdf;
    $pdf->Line($x,current_line() - 2,$x + $width,current_line()-2);
  }
   
// ---------------------------------------------------------------------------------------------------------------------  
  function gentab_Line($index=0,$extend=0) {gen_table_line($index,$extend);}
  function gen_table_line($index=0,$extend=0)
  {
    global $pdf;
    global $gen_tables;
    
    $pdf->Line
    (
      $gen_tables[$index]['x']                                         , current_line() - 2,
      $gen_tables[$index]['x'] + $gen_tables[$index]['width'] + $extend, current_line()-2
    );
  }
// ---------------------------------------------------------------------------------------------------------------------  
  function gentab_VerLine_Start($table_index=0,$line_index,$x)
  {
    global $gen_tables;
    $gen_tables[$table_index]['lines'][$line_index]['x']     = $x;
    $gen_tables[$table_index]['lines'][$line_index]['start'] = current_line();
  }
// ---------------------------------------------------------------------------------------------------------------------  
  function gentab_VerLine_End($table_index=0,$line_index)
  {
    global $pdf;
    global $gen_tables;
    
    $x = $gen_tables[$table_index]['x'] + $gen_tables[$table_index]['lines'][$line_index]['x'];
    $y = $gen_tables[$table_index]['lines'][$line_index]['start'] - 2;
    
    $pdf->Line($x,$y,$x,current_line() - 2);
  }
// ---------------------------------------------------------------------------------------------------------------------  
  function gentab_CenterText($index,$caption,$custom_x_offset=0,$custom_width=0)
  {
    gen_table_center_text($index,$caption,$custom_x_offset,$custom_width);
  }

  //merge later, her for compatibility
  function gen_table_center_text($index,$caption,$custom_x_offset=0,$custom_width=0)
  {
    global $pdf;
    global $gen_tables;
    
    $ct_x = $gen_tables[$index]['x'];
    $ct_width = $gen_tables[$index]['width'];
    
    if($custom_x_offset > 0) {$ct_x  += $custom_x_offset;}
    if($custom_width > 0) {$ct_width = $custom_width;}
    
    $pdf->CenterText($ct_x,current_line(),$caption,$ct_width);
  } 
  
// ---------------------------------------------------------------------------------------------------------------------  
  function gentab_Text($index=0,$caption,$left_right='L',$line_offset=0)
  {
    global $pdf;
    global $gen_tables;
    global $global_margin;
     
    $x = $gen_tables[$index]['x'];
    $y = current_line();
    
    if($left_right == 'L') {$x -= $gen_tables[$index]['txt_left'];}
    if($left_right == 'R') {$x += $gen_tables[$index]['width'] + $gen_tables[$index]['txt_right'];}
    
    if($line_offset != 0) {$y += ($line_offset * $global_margin['text_space']);}
     
    $pdf->Text($x,$y,$caption);
  }

// ---------------------------------------------------------------------------------------------------------------------  
  function gentab_Cell_Add($table_index,$cell_index,$width)
  {
    global $gen_tables;
    
    $gen_tables[$table_index]['cells'][$cell_index]['width'] = $width;
    $cell_x   = 0;

    for($loop = 0;$loop < $cell_index;$loop++)
    {
      $cell_x += $gen_tables[$table_index]['cells'][$loop]['width'];
    }
    gentab_VerLine_Start($table_index,$cell_index,$cell_x + $width);
  }
  
// ---------------------------------------------------------------------------------------------------------------------  
  function gentab_Cell_Clear($table_index)
  {
    global $gen_tables;

    unset($gen_tables[$table_index]['cells']);
  }
  
// ---------------------------------------------------------------------------------------------------------------------  
  function gentab_Cell_CenterText($table_index,$cell_index,$caption)
  {
    global $gen_tables;

    //$elements = count($gen_tables[$table_index]['cells']);
    $width    = $gen_tables[$table_index]['cells'][$cell_index]['width'];
    $cell_x   = 0;

    for($loop = 0;$loop < $cell_index;$loop++)
    {
      $cell_x += $gen_tables[$table_index]['cells'][$loop]['width'];
    }
    
    gentab_CenterText($table_index,$caption,$cell_x,$width);
  }
  
  function gentab_Cell_Text($table_index,$cell_index,$caption,$alignment='C')
  {
    global $gen_tables;
    global $pdf;

    $width    = $gen_tables[$table_index]['cells'][$cell_index]['width'];
    $table_x  = $gen_tables[$table_index]['x'];
    $cell_x   = 0;

    for($loop = 0;$loop < $cell_index;$loop++)
    {
      $cell_x += $gen_tables[$table_index]['cells'][$loop]['width'];
    }

    switch($alignment)
    {
      case 'L':
        $pdf->Text($table_x + $cell_x + 2,current_line(),$caption);
        break;
      case 'C':
        gentab_Cell_CenterText($table_index,$cell_index,$caption);
        break;
      case 'R':
        $pdf->Text($table_x + $cell_x + $width - $pdf->GetStringWidth($caption) - 2,current_line(),$caption);
        break;
    }
  }
  
  
  
  function gentab_Cell_AutoWidth($table_index=0)
  {
    global $gen_tables;
    $elements = count($gen_tables[$table_index]['cells']);
    $cell_x   = 0;

    for($loop = 0;$loop < $elements;$loop++)
    {
      $cell_x += $gen_tables[$table_index]['cells'][$loop]['width'];
    }

    $gen_tables[$table_index]['width'] = $cell_x;
  }

// ---------------------------------------------------------------------------------------------------------------------  
// ---------------------------------------------------------------------------------------------------------------------  
// ---------------------------------------------------------------------------------------------------------------------  
// ---------------------------------------------------------------------------------------------------------------------  
// ---------------------------------------------------------------------------------------------------------------------  
  
// This should be a separate library script but for now it lives here  
  function gentab_BitField_16($x,$y,$cell_width,$cell_height,$caption_high,$cells_high,$caption_low,$cells_low)
  {
    $x_border_right = 190; //$x + ($cell_width * 8) + ($cell_width / 2);
    
    bit_field_table($x,$y,$cell_width,$cell_height,$cells_high);
    bit_field_table($x + ($cell_width / 2),$y + $cell_height,$cell_width,$cell_height,$cells_low);
    caption_16_bit_field($x - 3,$x_border_right,$caption_high,$caption_low);
  }
  
  function gentab_BitField_8($x,$y,$cell_width,$cell_height,$cells)
  {
    global $pdf;
     
    $pdf->Text($x + 2,current_line() - 2,'MSB');
    $pdf->Text($x + ($cell_width * 8) - 2 - $pdf->GetStringWidth('LSB'),current_line() - 2,'LSB');
     
    bit_field_table($x,$y,$cell_width,$cell_height,$cells);
    new_line($y + $cell_height);
    new_line();
  }
  
  function caption_16_bit_field($x_offset_left,$x_offset_right,$caption_high='',$caption_low='')
  {
    global $pdf;
    
    $pdf->SetFont('arial','',8);
    new_line(current_line() + 2);
      $pdf->Text($x_offset_left - $pdf->GetStringWidth($caption_high) - 1,current_line(),$caption_high);
      $pdf->Text($x_offset_right - $pdf->GetStringWidth('(D15-D8)'),current_line(),'(D15-D8)');
    new_line(current_line() + (3.8 * 2));
      $pdf->Text($x_offset_left - $pdf->GetStringWidth($caption_low) - 1,current_line(),$caption_low);
      $pdf->Text($x_offset_right - $pdf->GetStringWidth('(D7-D0)'),current_line(),'(D7-D0)');
    new_line(current_line() + 3.8);
  }
   
  function bit_field_table($x,$y,$cell_width,$cell_height,$cells)
  {
    global $pdf;
    $pdf->Rect($x,$y,$cell_width * 8,$cell_height);
    
    for($bits=0;$bits<8;$bits++)
    {
      $pdf->Line($x + ($cell_width * $bits),$y,$x + ($cell_width * $bits),$y + $cell_height);
      $pdf->CenterText($x + ($cell_width * $bits),$y + ($cell_height - 2),$cells[$bits],$cell_width);
    }
  }

  
  
  
?>
